<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .bigDiv {
            margin: 50px auto;
            width: 200px;
            height: 200px;
            background-color: lightskyblue;
            border-radius: 10px;
        }
 
       h2 {
           
           text-align: center;
         
        }
        #showNum {
            width: 200px;
            height: 20px;
            background-color: orange;
           text-align: center;
            border-radius: 5px;
        }
    </style>
</head>
<body>
    <div class="bigDiv">
        <h2 alig="center">计时器</h2>
        <div id="showNum" alig="center">
            0
        </div>
        <br>
        <br>
        <div class="butDiv">&nbsp&nbsp&nbsp&nbsp
            <button type="button" id="start">开始</button>
            &nbsp
            <button type="button" id="stop">停止</button>
            &nbsp
            <button type="button" id="reset">复位</button>
            &nbsp
        </div>
    </div>
    <script>
        //定义显示的时间
        let int = null;
        /**
         * 开始  单击事件
         */
        document.getElementById("start").addEventListener('click', function () {
            if (int == null) {
                //设置定时器
                //每隔参数二毫秒执行一次参数一
                int = setInterval(startNum, 1000);
            }
        });
        /**
         * 停止   单击事件
         */
        document.getElementById("stop").addEventListener('click', function () {
            //清除定时器，
            clearInterval(int);
            int = null;
        });
        /**
         * 复位    单击事件
         */
        let num = 0;
        document.getElementById("reset").addEventListener('click', function () {
            if (int == null) {
                num = 0;
                //将时间变成0
                document.getElementById("showNum").innerHTML = num;
            }
        });
     
        function startNum() {
            num++;
            //持续改变时间
            document.getElementById("showNum").innerHTML = num;
        }
    </script>
</body>
</html>